//
//  GHUtils.m
//  GodHorses
//
//  Created by Mac on 2017/11/14.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "TMUtils.h"

@implementation TMUtils
+ (void)netWorkMonitorinCanUseWithHandle:(void (^)())handle{
    
    AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
    [manager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        if (status == AFNetworkReachabilityStatusReachableViaWWAN || status == AFNetworkReachabilityStatusReachableViaWiFi) {
            if (handle) {
                handle();
            }
        }
    }];
    [manager startMonitoring];
}
+ (UIView*)headerViewWithTitle:(NSString*)title{
    UIView * sectionHeader = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 44)];
    sectionHeader.backgroundColor = [UIColor whiteColor];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 44)
                                                     text:title
                                                textColor:TM_firstTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    
    [sectionHeader addSubview:titleLable];
    return sectionHeader;
}
+ (UINavigationBar *)navigationBar{
    UINavigationController* nav = (id)[UIApplication sharedApplication].keyWindow.rootViewController.childViewControllers.firstObject;
    return nav.navigationBar;
}
+  (UITabBar *)tabbar{
    UITabBarController* tab = (id)[UIApplication sharedApplication].keyWindow.rootViewController;
    return tab.tabBar;
}
+ (CGFloat)navgationBarBootom{
    if (KIsiPhoneX) {
        return 88;
    }
    return 64;
}
+ (CGFloat)tabBarTop{
    if (KIsiPhoneX) {
        return (UIScreenHeight - 83);
    }
    return (UIScreenHeight - 49);
}
+ (void)debugSimulationNetWorkWithHadle:(void (^)(NSInteger, NSMutableArray *))handle{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSInteger num = 1;
    NSInteger randrom = arc4random()%100;
    NSMutableArray * array = [NSMutableArray new];
    for (NSInteger i = 0; i < randrom; i ++) {
        [array addObject:@(i)];
    }
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(num * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [SVProgressHUD dismiss];
        if (handle) {
            handle(num,array);
        }
    });
}
+ (void)sendNoJsonRequestWithUrl:(NSString *)url params:(NSDictionary *)params isPost:(BOOL)isPost handle:(void (^)(BOOL, NSString *))handle{
    AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
    manger.requestSerializer = [AFHTTPRequestSerializer serializer];
    manger.responseSerializer = [AFHTTPResponseSerializer serializer];
    manger.requestSerializer.timeoutInterval = 20;
    manger.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",@"text/json", @"text/plain", @"text/html", nil];
    if (isPost) {
        [manger POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            if (handle) {
                handle(YES,string);
            }
            [SVProgressHUD dismiss];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (handle) {
                handle(NO,nil);
            }
            [SVProgressHUD dismiss];
        }];
    }else{
        [manger GET:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            NSString * string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
            if (handle) {
                handle(YES,string);
            }
            [SVProgressHUD dismiss];
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            if (handle) {
                handle(NO,nil);
            }
            [SVProgressHUD dismiss];
        }];
    }
    
    
}
+ (UIView *)normalTextField:(UITextField *)textFiled title:(NSString *)title{
    UIView * view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 70)];
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(15, 0, UIScreenWidth - 30, 35)
                                                     text:title
                                                textColor:SecendTextColor
                                                     font:18
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [view addSubview:titleLable];
    
    textFiled.frame = CGRectMake(titleLable.left, titleLable.bottom, titleLable.width, titleLable.height);
    textFiled.keyboardType = UIKeyboardTypeASCIICapable;
    textFiled.placeholder = [NSString stringWithFormat:@"请输入%@",title];
    textFiled.textColor = ThirdTextColor;
    [textFiled st_showBottomLine];
    [view addSubview:textFiled];
    return view;
}
+ (void)autoLoginWithAccount:(NSString *)account pwd:(NSString *)pwd{
    
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    if (account.length) {
        [paramDic setObject:account forKey:@"user_name"];
    }
    if (pwd.length) {
        [paramDic setObject:pwd forKey:@"password"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Rest/Login/login"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSDictionary * modelDic = [responseObject valueForKey:@"data"];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\nurl:%@\nparam:%@\nreson:%@",url,paramDic,error.desc);
                                             }];
}
+(void)fecthUserModelWithUserId:(NSString *)userIds handle:(void (^)(NSArray<MCUserModel *> *))handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    if (userIds.length) {
        [paramDic setObject:userIds forKey:@"uids"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Rest/User/getUinfo"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 NSMutableArray * array = [NSMutableArray new];
                                                 NSArray * arrarData = responseObject[@"data"];
                                                 for (NSDictionary * modelDic in arrarData) {
                                                     MCUserModel * user = [MCUserModel mj_objectWithKeyValues:modelDic];
                                                     [array addObject:user];
                                                 }
                                                 if (handle) {
                                                     handle(array);
                                                 }
                                                 
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [SVProgressHUD dismiss];
                                                 if (handle) {
                                                     handle(nil);
                                                 }
                                                 DDLogError(@"\nurl:%@\nparam:%@\nreson:%@",url,paramDic,error.desc);
                                             }];
}
+ (void)sendMyInfoRequestWithHandle:(void (^)(MCUserModel *))handle{
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    MCUserModel * user = STUserManger.defult.loginedUser;
    if (user.userID.length) {
        [paramDic setObject:user.userID forKey:@"uid"];
    }
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"/Rest/System/personalSetting"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [STNetWrokManger.defaultClient requestIgnoreSuccessWithPath:url
                                                         method:STHttpRequestTypePost
                                                     parameters:paramDic
                                                        success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                            
                                                            MCUserModel * netuser = [MCUserModel mj_objectWithKeyValues:responseObject];
                                                            user.user_icon = netuser.user_icon;
                                                            user.user_nick = netuser.user_nick;
                                                            user.province = netuser.province;
                                                            user.province_id = netuser.province_id;
                                                            user.city_id = netuser.city_id;
                                                            user.city = netuser.city;
                                                            [STUserManger.defult updateUserModel:user];
                                                            if (handle) {
                                                                handle(user);
                                                            }
                                                            
                                                            DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                            
                                                        } failure:^(NSString *stateCode, STError *error) {
                                                            [SVProgressHUD dismiss];
                                                            if (handle) {
                                                                handle(user);
                                                            }
                                                            DDLogError(@"\nurl:%@\nparam:%@\nreson:%@",url,paramDic,error.desc);
                                                        }];
}
@end

